package com.ihr360.job.core.lanuch.support;

import com.ihr360.job.core.JobParameters;
import com.ihr360.job.core.JobParametersBuilder;
import com.ihr360.job.core.JobParametersIncrementer;

public class RunIdIncrementer implements JobParametersIncrementer {

    private static String RUN_ID_KEY = "run.id";

    private String key = RUN_ID_KEY;

    /**
     * The name of the run id in the job parameters.  Defaults to "run.id".
     *
     * @param key the key to set
     */
    public void setKey(String key) {
        this.key = key;
    }

    /**
     * Increment the run.id parameter (starting with 1).
     */
    @Override
    public JobParameters getNext(JobParameters parameters) {

        JobParameters params = (parameters == null) ? new JobParameters() : parameters;

        long id = params.getLong(key, 0L) + 1;
        return new JobParametersBuilder(params).addLong(key, id).toJobParameters();
    }

}
